package com.itheima.home_work.workman;

import java.util.Map;
import java.util.TreeMap;

public class Test {
    public static void main(String[] args) {
        /**
         * 请使用TreeMap集合保存劳模信息,要求以劳模对象为键,
         * 家庭住址为值,并按照劳模的年龄从大到小排序后输出;
         */

        TreeMap<Worker, String> workers = new TreeMap<>((wrk1, wrk2) -> wrk2.getAge() - wrk1.getAge());

        workers.put(new Worker("张三", 18), "北京");
        workers.put(new Worker("李四", 20), "上海");
        workers.put(new Worker("王五", 35), "天津");
        workers.put(new Worker("赵六", 21), "北京");
        workers.put(new Worker("田七", 19), "上海");

        workers.forEach((k, v) -> System.out.println(k + " --> " + v));
        System.out.println("-------------");

        // 1、劳模类中有姓名和年龄两个属性

        // 2、添加上述信息后,使用代码删除张三的信息
        workers.remove(new Worker("张三", 18));

        // 3、添加上述信息后,使用代码修改李四的家庭住址为郑州
        workers.put(new Worker("李四", 20), "郑州");
        workers.forEach((k, v) -> System.out.println(k + " --> " + v));

        // 4、使用至少两种方式遍历集合中的信息并输出
        System.out.println("方式1遍历：");
        for (Worker worker: workers.keySet()) {
            System.out.println(worker.getAge() + "岁的" +worker.getName() + "--" + workers.get(worker));
        }
        System.out.println("方式2遍历：");
        for (Map.Entry entry: workers.entrySet()) {
            Worker worker = (Worker) entry.getKey();
            System.out.println(worker.getAge() + "岁的" +worker.getName() + "--" + entry.getValue());
        }
        System.out.println("方式3遍历：");
        workers.forEach((k, v) -> System.out.println(k.getAge() + "岁的" +k.getName() + "--" + v));
    }
}
